import { createRouter, createWebHistory } from 'vue-router';
import store from '@/store'



let routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '@/views/Login/index.vue' )
  },
  {
    path: '/home',
    name: 'home',
    component: () => import( '@/views/Home/index.vue' ),
    children: [
      {
        path: '/',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import( '@/views/Welcome/index.vue' )
      },
      {
        path: '/users',
        name: 'users',
        component: () => import( '@/views/Users/index.vue' )
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import( '@/views/Roles/index.vue' )
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import( '@/views/Rights/index.vue' )
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import( '@/views/Goods/index.vue' )
      },
      {
        path: '/params',
        name: 'params',
        component: () => import( '@/views/Params/index.vue' )
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import( '@/views/Categories/index.vue' )
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import( '@/views/Orders/index.vue' )
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import( '@/views/Reports/index.vue' )
      },

    ]
  },
]

let router = createRouter( {
  history: createWebHistory(),
  routes
} )


router.beforeEach( ( to, from, next ) => {
  console.log( store );
  if ( to.path === '/login' ) {
    if ( store.state.token ) {
      next( '/home' );
    } else {
      next();
    }
  } else {
    if ( store.state.token ) {
      next();
    } else {
      next( '/login' );
    }
  }
} )

export default router;